package demo;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) {
        //查询
        //select();
        //新增
//        add();
        //修改
       // update();
        //删除
        //delete();
//        selectByXPath();
        testAsXml();
    }
    public static void select(){
        //1.创建解析器SAX
        SAXReader reader=new SAXReader();
        //2.读取xml文件得到document对象
        try {
            Document document = reader.read("ch02/src/book.xml");
            //3.获取根标签
            Element rootElement = document.getRootElement();
            //4.获取根标签下面的子标签
            List<Element> books = rootElement.elements("book");
            //5.遍历
            for (Element book : books) {
                System.out.println("==================================");
                //获取属性
                String id = book.attributeValue("id");
                System.out.println("属性id的值："+id);

                Element title = book.element("title");
                System.out.println("title标签中间的文本："+title.getText());
                Element author = book.element("author");
                System.out.println("author标签中间的文本："+author.getText());
                Element description = book.element("description");
                System.out.println("description标签中间的文本："+description.getText());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void add(){
        XMLWriter writer=null;
        //1.创建解析器
        SAXReader reader=new SAXReader();
        //2.读取xml文件得到document对象
        try {
            Document document=reader.read("ch02/src/book.xml");
            //3.获取根节点
            Element rootElement = document.getRootElement();
            //4.根据根标签获取下面的子标签
            List<Element> book = rootElement.elements("book");
            //5.根据下标获取指定的标签
            Element bookElement = book.get(1);
            //6.在book标签下面加一个子标签price
            Element priceElement = bookElement.addElement("price");
            //7.设置标签中间的文本
            priceElement.setText("88.88");
            //8.输出文件
            OutputFormat prettyPrint = OutputFormat.createPrettyPrint();
            prettyPrint.setEncoding("UTF-8");

            writer=new XMLWriter(new FileWriter("ch02/src/book.xml"),prettyPrint);
            writer.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void update(){
        XMLWriter writer=null;
        //1.创建解析器
        SAXReader reader=new SAXReader();
        //2.读取xml文件得到document对象
        Document document= null;
        try {
            document = reader.read("ch02/src/book.xml");
            //3.获取根节点
            Element rootElement = document.getRootElement();
            //4.根据根标签获取下面的子标签
            List<Element> book = rootElement.elements("book");
            //5.通过下标获取第一个book标签
            Element bookElement = book.get(0);
            //6.通过book标签获取它下面子标签author
            Element authorElement = bookElement.element("author");
            //7.设置文本
            authorElement.setText("张大俊");

            //8.输出文件
            OutputFormat prettyPrint = OutputFormat.createPrettyPrint();
            prettyPrint.setEncoding("UTF-8");

            writer=new XMLWriter(new FileWriter("ch02/src/book.xml"),prettyPrint);
            writer.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void delete(){
        XMLWriter writer=null;
        //1.创建解析器
        SAXReader reader=new SAXReader();
        //2.读取xml文件得到document对象
        Document document= null;
        try {
            document = reader.read("ch02/src/book.xml");
            //3.获取根节点
            Element rootElement = document.getRootElement();
            //4.根据根标签获取下面的子标签
            List<Element> book = rootElement.elements("book");
            //5.获取第二个book标签
            Element bookElement = book.get(1);
            //6.获取book标签的子标签price
            Element priceElement = bookElement.element("price");
            //7.父节点删子节点
            bookElement.remove(priceElement);

            //8.输出文件
            OutputFormat prettyPrint = OutputFormat.createPrettyPrint();
            prettyPrint.setEncoding("UTF-8");

            writer=new XMLWriter(new FileWriter("ch02/src/book.xml"),prettyPrint);
            writer.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void selectByXPath(){
        //1.创建解析器SAX
        SAXReader reader=new SAXReader();
        //2.读取xml文件得到document对象
        try {
            Document document = reader.read("ch02/src/book.xml");
            //3.获取根标签
            Element rootElement = document.getRootElement();

          //List<Element> list = rootElement.selectNodes("/books/book/author");
            List<Element> list = rootElement.selectNodes("//author");
            for (Element element : list) {
                System.out.println(element.getText());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testAsXml(){
        //1.创建解析器SAX
        SAXReader reader=new SAXReader();
        //2.读取xml文件得到document对象
        try {
            Document document = reader.read("ch02/src/book.xml");
            //3.xml文件转换为字符串
            System.out.println(document.asXML());

            String str="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<books> \n" +
                    "  <!-- 图书信息 -->  \n" +
                    "  <book id=\"bk101\"> \n" +
                    "    <title>九阳神功</title>  \n" +
                    "    <author>张大俊</author>  \n" +
                    "    <description>九阳神功真本书不错</description> \n" +
                    "  </book>  \n" +
                    "  <book id=\"bk102\"> \n" +
                    "    <title>葵花宝典</title>  \n" +
                    "    <author>东方不败</author>  \n" +
                    "    <description>欲练此功；.....</description>  \n" +
                    "    <!--<price>88.88</price>--> \n" +
                    "  </book> \n" +
                    "</books>";

            //4.将xml的字符串转换为xml
            Document document1=DocumentHelper.parseText(str);
            System.out.println(document1.getRootElement().getName());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
